<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 部门模型
 */
class Dept extends BaseModel
{
    // 定义软删除
    use SoftDelete;

    protected array $allowModifyFields = ['status', 'sort'];

    protected array $fields = ['id', 'parent_id', 'id' => 'value', 'name' => 'label'];

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->query->order('sort', 'desc');
    }

    /**
     * 通过中间表获取角色
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_dept', 'role_id');
    }
}